package com.ruoyi.myplayer.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.myplayer.mapper.PlayerSongMapper;
import com.ruoyi.util.HttpClientExample;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.myplayer.domain.PlayerSong;
import com.ruoyi.myplayer.service.IPlayerSongService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * @description 歌曲信息
 * @author lvyq
 * @param[1] null
 * @throws
 * @time 2025-04-11 15:38
 */
@RestController
@RequestMapping("/myplayer/song")
public class PlayerSongController extends BaseController
{
    @Resource
    private IPlayerSongService playerSongService;

    @Resource
    private PlayerSongMapper playerSongMapper;

    /**
     * 查询歌曲列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PlayerSong playerSong)
    {
        startPage();
        List<PlayerSong> list = playerSongService.selectPlayerSongList(playerSong);
        return getDataTable(list);
    }

    @GetMapping("/page")
    public TableDataInfo listAll(PlayerSong playerSong)
    {
        startPage();
        List<PlayerSong> list = playerSongService.selectPlayerSongPage(playerSong);
        return getDataTable(list);
    }


    @GetMapping("/search/{searchVal}")
    public AjaxResult search(@PathVariable("searchVal") String searchVal)
    {
        List<Map<String,Object>> list= playerSongService.search(searchVal);
        return AjaxResult.success(list);
    }


    /**
     * 获取歌曲详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(playerSongService.selectPlayerSongById(id));
    }

    /**
     * 新增歌曲
     */
    @Log(title = "歌曲", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlayerSong playerSong)
    {
        return toAjax(playerSongService.insertPlayerSong(playerSong));
    }

    /**
     * 修改歌曲
     */
    @Log(title = "歌曲", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlayerSong playerSong)
    {
        return toAjax(playerSongService.updatePlayerSong(playerSong));
    }

    /**
     * 删除歌曲
     */
    @Log(title = "歌曲", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(playerSongService.deletePlayerSongByIds(ids));
    }


    /**
     * @description 音源守护更新
     * @author lvyq
     * @param[1] id
     * @throws
     * @time 2025-04-11 15:18
     */
    @GetMapping("/reUpdSong")
    public String  reUpdSong(@RequestParam("id") Long id){
        PlayerSong song = reUpdSongUrl(id);
        return song.getSong();
    }

    private PlayerSong reUpdSongUrl(Long id) {
        PlayerSong song = playerSongService.selectPlayerSongById(id);

        if (song!=null){
            Date nowDate =  new Date();
            Date createTime = song.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long time = nowDate.getTime() -  createTime.getTime();
            long m = time/1000/60;
            if (m<60*2){
                return song;
            }

                if (song.getReNum()<0  ){
                    return song;
                }

            PlayerSong newSong = HttpClientExample.getNewSong(song.getOnkey());
                if (newSong!=null){
                    song.setSong(newSong.getSong());
                }

                //标识为损坏，便于手动查库，手动适配合适的音源
                song.setReUpdate(1l);
                if (song.getReNum()==null){
                    song.setReNum(1l);
                }else {
                    song.setReNum(song.getReNum()+1);
                }
                song.setCreateTime(new Date());
                playerSongService.updatePlayerSong(song);
            }
        return song;


    }


    /**
     * @description 获取最新的单曲
     * @author lvyq
     * @throws
     * @return AjaxResult
     * @time 2025-04-15 16:58
     */
    @GetMapping("/getNewSong")
    public AjaxResult getNewSong(){
     List<Map<String,Object>> list= playerSongService.getNewSong();
     return AjaxResult.success(list);
    }

    public static void main(String[] args) throws ParseException {
        Date nowDate =  new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = format.parse("2025-04-21 14:44:00");
        Date createTime2 = format.parse("2025-04-24 14:44:01");
        long time = createTime.getTime();
        long time2 = createTime.getTime();
        long l = nowDate.getTime() - time;

        System.out.println("l = " + l/1000/60);

    }


    /**
     * @description 版本检测升级
     * @author lvyq
     * @param[1] version
     * @throws
     * @return AjaxResult
     * @time 2025-05-13 14:41
     */
    @GetMapping("/checkVersion/{versionCode}")
    public AjaxResult upVersion(@PathVariable ("versionCode") Integer versionCode){
        Map<String,Object> result =playerSongMapper.upVersion(versionCode);
        return AjaxResult.success(result);
    }



}
